using System;
using System.Collections.Generic;
using System.Configuration;
using System.Xml;

namespace TestAdbCommand
{
    public class AdbCommandConfig
    {
        public string Name { get; set; }
        public string Value { get; set; }
        public int Type { get; set; }

        public static List<AdbCommandConfig> LoadCommands()
        {
            var commands = new List<AdbCommandConfig>();
            
            try
            {
                var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                var section = config.GetSection("AdbCommands");
                
                if (section != null)
                {
                    var xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(section.SectionInformation.GetRawXml());
                    
                    var commandNodes = xmlDoc.SelectNodes("//Command");
                    if (commandNodes != null)
                    {
                        foreach (XmlNode node in commandNodes)
                        {
                            if (node is XmlElement element)
                            {
                                var command = new AdbCommandConfig
                                {
                                    Name = element.GetAttribute("name"),
                                    Type = int.Parse(element.GetAttribute("type") ?? "1")
                                };

                                // 处理value节点
                                var valueNode = element.SelectSingleNode("value");
                                if (valueNode != null)
                                {
                                    command.Value = valueNode.InnerText;
                                }
                                else
                                {
                                    command.Value = element.GetAttribute("value");
                                }

                                commands.Add(command);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show($"加载命令配置时发生错误：{ex.Message}", "错误", 
                    System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }

            return commands;
        }
    }
} 